package com.snopy.leetcode.index1_1000.index401_500;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Question429
 * @description N叉树的层序遍历
 *
 * @email 77912204@qq.com
 * @date 2022/04/08 12:26
 */
public class Question429 {

    public static void main(String[] args) {
        List<List<Integer>> list = levelOrder(null);
    }
    public static List<List<Integer>> levelOrder(Node root) {
        LinkedList<Node> stack = new LinkedList<>();
        List<List<Integer>> list = new ArrayList<>();
        if (root==null){
            return new ArrayList<>();
        }
        stack.add(root);
        while (!stack.isEmpty()){
            int size = stack.size();
            List<Integer> tmpList = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Node node = stack.pop();
                if (node!=null){
                    tmpList.add(node.val);
                    stack.addAll(node.children);
                }
            }
            list.add(tmpList);
        }
        return list;
    }

}
class Node{
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
}